package by.gravity.pvh.fragments;

import org.holoeverywhere.LayoutInflater;
import org.holoeverywhere.widget.ArrayAdapter;
import org.holoeverywhere.widget.EditText;
import org.holoeverywhere.widget.LinearLayout;
import org.holoeverywhere.widget.Spinner;
import org.holoeverywhere.widget.Toast;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import by.gravity.pvh.R;
import by.gravity.pvh.data.SQLDataManager.TypeOrder;
import by.gravity.pvh.model.BalconyBlockModel;
import by.gravity.pvh.model.CommonModel;
import by.gravity.pvh.utils.Configuration;
import by.gravity.pvh.utils.Formula;
import by.gravity.pvh.utils.Formula.TypeDoor;
import by.gravity.pvh.utils.Formula.TypeWindow;

public class BalconyBlockFragment extends CommonFragment {

	private EditText windowWidth;

	private EditText windowHeight;

	private RadioGroup typeOpenWindowGroup;

	private RadioGroup typeWindowGroup;

	private EditText countSimpleOpen;

	private EditText countWithWind;

	private EditText countDeafParts;

	private EditText underWindowLength;

	private Spinner underWindowWidth;

	private EditText windowEbbDownLength;

	private Spinner windowEbbDownWidth;

	private EditText underWindowWindowOutLength;

	private Spinner underWindowWindowOutWidth;

	private EditText underWindowWindowInLength;

	private Spinner underWindowWindowInWidth;

	private EditText underWindowDoorOutLength;

	private Spinner underWindowDoorOutWidth;

	private EditText underWindowDoorInLength;

	private Spinner underWindowDoorInWidth;

	private EditText doorWidth;

	private EditText doorHeight;

	private RadioGroup typeDoorGroup;

	// private RadioGroup typeDoorOpenWindowGroup;

	private EditText underWindowDoorLength;

	private Spinner underWindowDoorWidth;

	private EditText workPrice;

	private RadioGroup underWindowGroupType;

	private RadioGroup doorType;

	private RadioGroup additionalWindowType;

	private LinearLayout aditionalTypeWindowLayout;

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {

		super.onActivityCreated(savedInstanceState);
		init();

	}

	private void init() {

		windowWidth = (EditText) getView().findViewById(R.id.windowWidth);
		windowHeight = (EditText) getView().findViewById(R.id.windowHeight);
		typeOpenWindowGroup = (RadioGroup) getView().findViewById(R.id.typeOpenWindowGroup);
		typeWindowGroup = (RadioGroup) getView().findViewById(R.id.typeWindowGroup);
		countSimpleOpen = (EditText) getView().findViewById(R.id.countTypeSimpleOpen);
		countWithWind = (EditText) getView().findViewById(R.id.countTypeWind);
		countDeafParts = (EditText) getView().findViewById(R.id.countDeafParts);

		doorWidth = (EditText) getView().findViewById(R.id.doorWidth);
		doorHeight = (EditText) getView().findViewById(R.id.doorHeight);
		// typeDoorOpenWindowGroup = (RadioGroup)
		// getView().findViewById(R.id.typeDoorOpenWindowGroup);
		typeDoorGroup = (RadioGroup) getView().findViewById(R.id.typeDoorGroup);

		underWindowLength = (EditText) getView().findViewById(R.id.underWindowLength);
		underWindowWidth = (Spinner) getView().findViewById(R.id.underWindowWidth);
		underWindowWidth.setAdapter(new ArrayAdapter<Integer>(getActivity(),
				org.holoeverywhere.R.layout.support_simple_spinner_dropdown_item, Configuration.getUnderwindowwidth()));

		windowEbbDownLength = (EditText) getView().findViewById(R.id.windowEbbDownLength);
		windowEbbDownWidth = (Spinner) getView().findViewById(R.id.windowEbbDownWidth);
		windowEbbDownWidth.setAdapter(new ArrayAdapter<Integer>(getActivity(),
				org.holoeverywhere.R.layout.support_simple_spinner_dropdown_item, Configuration.getEbbwidth()));

		underWindowDoorLength = (EditText) getView().findViewById(R.id.underWindowDoorLength);
		underWindowDoorWidth = (Spinner) getView().findViewById(R.id.underWindowDoorWidth);
		underWindowDoorWidth.setAdapter(new ArrayAdapter<Integer>(getActivity(),
				org.holoeverywhere.R.layout.support_simple_spinner_dropdown_item, Configuration.getUnderwindowwidth()));

		underWindowWindowOutLength = (EditText) getView().findViewById(R.id.underWindowOutLength);

		underWindowWindowOutWidth = (Spinner) getView().findViewById(R.id.underWindowOutWidth);
		underWindowWindowOutWidth.setAdapter(new ArrayAdapter<Integer>(getActivity(),
				org.holoeverywhere.R.layout.support_simple_spinner_dropdown_item, Configuration.getUnderwindowwidth()));

		underWindowWindowInLength = (EditText) getView().findViewById(R.id.underWindowInLength);

		underWindowWindowInWidth = (Spinner) getView().findViewById(R.id.underWindowInWidth);
		underWindowWindowInWidth.setAdapter(new ArrayAdapter<Integer>(getActivity(),
				org.holoeverywhere.R.layout.support_simple_spinner_dropdown_item, Configuration.getUnderwindowwidth()));
		underWindowDoorOutLength = (EditText) getView().findViewById(R.id.underwindowDoorOutLength);

		underWindowDoorOutWidth = (Spinner) getView().findViewById(R.id.underWindowDoorOutWidth);
		underWindowDoorOutWidth.setAdapter(new ArrayAdapter<Integer>(getActivity(),
				org.holoeverywhere.R.layout.support_simple_spinner_dropdown_item, Configuration.getUnderwindowwidth()));

		underWindowDoorInLength = (EditText) getView().findViewById(R.id.underWindowDoorInLength);

		underWindowDoorInWidth = (Spinner) getView().findViewById(R.id.underWindowDoorInWidth);
		underWindowDoorInWidth.setAdapter(new ArrayAdapter<Integer>(getActivity(),
				org.holoeverywhere.R.layout.support_simple_spinner_dropdown_item, Configuration.getUnderwindowwidth()));

		workPrice = (EditText) getView().findViewById(R.id.workPrice);

		underWindowGroupType = (RadioGroup) getView().findViewById(R.id.underWindowGroup);

		String underWindowValue = getValue(underWindowGroupType);

		underWindowGroupType.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				String checkedValue = getValue(group);
				selectUnderwindowGroupType(checkedValue);
			}
		});

		selectUnderwindowGroupType(underWindowValue);

		typeWindowGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				if (!getCountWindow().equals("")) {
					int countWindow = getIntValue(getCountWindow());
					showWindowImage(countWindow);
					if (countWindow == 2) {
						aditionalTypeWindowLayout.setVisibility(View.VISIBLE);
					} else {
						aditionalTypeWindowLayout.setVisibility(View.GONE);
					}
				} else {
					aditionalTypeWindowLayout.setVisibility(View.GONE);
				}
			}
		});

		doorType = (RadioGroup) getView().findViewById(R.id.doorType);

		additionalWindowType = (RadioGroup) getView().findViewById(R.id.aditionalTypeWindow);

		aditionalTypeWindowLayout = (LinearLayout) getView().findViewById(R.id.aditionalTypeWindowLayout);
	}

	private void selectUnderwindowGroupType(String underWindowValue) {
		View doubleUnderwindowLayout = getView().findViewById(R.id.doubleUnderwindowLayout);
		View underwindowWithEbbLayout = getView().findViewById(R.id.underwindowWithEbbLayout);
		if (underWindowValue.equals(getString(R.string.underwindowTypeDouble))) {
			doubleUnderwindowLayout.setVisibility(View.VISIBLE);
			underwindowWithEbbLayout.setVisibility(View.GONE);
		} else if (underWindowValue.equals(getString(R.string.underwindowTypeWithEbb))) {
			doubleUnderwindowLayout.setVisibility(View.GONE);
			underwindowWithEbbLayout.setVisibility(View.VISIBLE);
		} else {
			doubleUnderwindowLayout.setVisibility(View.GONE);
			underwindowWithEbbLayout.setVisibility(View.GONE);
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		return inflater.inflate(R.layout.f_balcony_block, null);
	}

	@Override
	protected String getFormula() {

		String windowWidth = this.windowWidth.getText().toString();
		String windowHeight = this.windowHeight.getText().toString();
		String typeWindowString = getValue(this.typeOpenWindowGroup);
		if (typeWindowString.equals("")) {
			Toast.makeText(getActivity(), R.string.notSelectedTypeWindow, Toast.LENGTH_SHORT).show();
			return null;
		}
		TypeWindow typeWindow = typeWindowString.equals(getString(R.string.oneChamger)) ? TypeWindow.ONE_CHAMGER : TypeWindow.TWO_CHAMGER;
		String countWindow = getValue(this.typeWindowGroup);
		if (countWindow.equals("")) {
			Toast.makeText(getActivity(), R.string.notSelectedWindow, Toast.LENGTH_SHORT).show();
			return null;
		}
		countWindow = getCountWindow();
		String countSimpleOpen = this.countSimpleOpen.getText().toString();
		String countWithWind = this.countWithWind.getText().toString();
		String countDeafParts = this.countDeafParts.getText().toString();
		String underwindowLength = underWindowLength.getText().toString();
		String underwindowWidth = String.valueOf(underWindowWidth.getSelectedItemPosition() + 1);
		String ebbDownLength = windowEbbDownLength.getText().toString();
		String ebbDownWidth = String.valueOf(windowEbbDownWidth.getSelectedItemPosition() + 1);

		String workPrice = this.workPrice.getText().toString();

		if (countSimpleOpen.length() == 0) {
			countSimpleOpen = "0";
		}
		if (countWithWind.length() == 0) {
			countWithWind = "0";
		}
		if (countDeafParts.length() == 0) {
			countDeafParts = "0";
		}

		if (Integer.parseInt(countWindow) != Integer.parseInt(countSimpleOpen) + Integer.valueOf(countWithWind)
				+ Integer.valueOf(countDeafParts)) {
			Toast.makeText(getActivity(), R.string.windowCountError, Toast.LENGTH_SHORT).show();
			return null;
		}

		String doorWidth = this.doorWidth.getText().toString();
		String doorHeight = this.doorHeight.getText().toString();
		// TypeWindow typeDoorWindow =
		// getRadioGroupValue(this.typeDoorOpenWindowGroup).equals(getString(R.string.oneChamger))
		// ? TypeWindow.ONE_CHAMGER
		// : TypeWindow.TWO_CHAMGER;
		String typeDoorString = getValue(this.typeDoorGroup);
		if (typeDoorString.equals("")) {
			Toast.makeText(getActivity(), R.string.notSelectedTypeDoor, Toast.LENGTH_SHORT).show();
			return null;
		}
		TypeDoor typeDoor = TypeDoor.WITH_OPEN;
		if (typeDoorString.equals(getString(R.string.doorTypeWithOpen))) {
			typeDoor = TypeDoor.WITH_OPEN;
		} else if (typeDoorString.equals(getString(R.string.doorTypeWithOpenAndWind))) {
			typeDoor = TypeDoor.WITH_OPEN_AND_WIND;
		}

		String underwindowDoorLength = underWindowDoorLength.getText().toString();
		String underwindowDoorWidth = String.valueOf(underWindowDoorWidth.getSelectedItemPosition() + 1);

		if (getValue(this.underWindowGroupType).equals("")) {
			Toast.makeText(getActivity(), R.string.notSelectCountUnderwindow, Toast.LENGTH_SHORT).show();
		}

		String underwindowWindowOutWidth = getValue(underWindowWindowOutWidth);
		String underwindowWindowOutLength = getValue(underWindowWindowOutLength);
		String underwindowWindowInWidth = getValue(underWindowWindowInWidth);
		String underwindowWindowInLength = getValue(underWindowWindowInLength);
		String underwindowDoorOutWidth = getValue(underWindowDoorOutWidth);
		String underwindowDoorOutLength = getValue(underWindowDoorOutLength);
		String underwindowDoorInWidth = getValue(underWindowDoorInWidth);
		String underwindowDoorInLength = getValue(underWindowDoorInLength);
		String underWindowGroupType = getValue(this.underWindowGroupType);

		Integer underWindowWidthInt = getIntValue(windowWidth);
		Integer underWindowDoorWidthInt = getIntValue(doorWidth);
		if ((getValue(this.underWindowGroupType)).equals(getString(R.string.underwindowTypeDouble))) {
			if (underWindowWidthInt > getIntValue(underWindowWindowOutLength)
					|| underWindowWidthInt > getIntValue(underWindowWindowInLength)) {
				Toast.makeText(getActivity(), R.string.underwindowLengthLess, Toast.LENGTH_SHORT).show();
				return null;
			}

			if (underWindowDoorWidthInt > getIntValue(underWindowDoorOutLength)
					|| underWindowDoorWidthInt > getIntValue(underWindowDoorInLength)) {
				Toast.makeText(getActivity(), R.string.underwindowLengthLessDoor, Toast.LENGTH_SHORT).show();
				return null;
			}
		} else {
			if (underWindowWidthInt > getIntValue(underWindowLength)) {
				Toast.makeText(getActivity(), R.string.underwindowLengthLess, Toast.LENGTH_SHORT).show();
				return null;
			} else if (underWindowDoorWidthInt > getIntValue(underwindowDoorLength)) {
				Toast.makeText(getActivity(), R.string.underwindowLengthLessDoor, Toast.LENGTH_SHORT).show();
				return null;
			} else if (underWindowWidthInt > getIntValue(ebbDownLength)) {
				Toast.makeText(getActivity(), R.string.ebbLengthLess, Toast.LENGTH_SHORT).show();
				return null;
			}
		}

		return Formula.balconyBlock(getActivity(), windowWidth, windowHeight, typeWindow, countWindow, countSimpleOpen, countWithWind,
				countDeafParts, underwindowLength, underwindowWidth, ebbDownLength, ebbDownWidth, doorWidth, doorHeight, typeWindow,
				typeDoor, underwindowDoorLength, underwindowDoorWidth, workPrice, underwindowWindowOutWidth, underwindowWindowOutLength,
				underwindowWindowInWidth, underwindowWindowInLength, underwindowDoorOutWidth, underwindowDoorOutLength,
				underwindowDoorInWidth, underwindowDoorInLength, underWindowGroupType);
	}

	private String getCountWindow() {
		String countWindow = getValue(this.typeWindowGroup);
		if (countWindow.equals(getString(R.string.oneOpen))) {
			countWindow = "1";
		} else if (countWindow.equals(getString(R.string.twoOpen))) {
			countWindow = "2";
		}

		return countWindow;
	}

	@Override
	protected CommonModel getModel() {
		BalconyBlockModel model = new BalconyBlockModel();
		model.setCountDeafParts(getValue(countDeafParts));
		model.setCountSimpleOpen(getValue(countSimpleOpen));
		model.setCountWithWind(getValue(countWithWind));
		model.setDoorHeight(getValue(doorHeight));
		model.setDoorWidth(getValue(doorWidth));
		model.setTypeDoorGroup(getValue(typeDoorGroup));
		model.setTypeOpenWindowGroup(getValue(typeOpenWindowGroup));
		model.setTypeWindowGroup(getValue(typeWindowGroup));
		model.setUnderWindowDoorInLength(getValue(underWindowDoorInLength));
		model.setUnderWindowDoorInWidth(getValue(underWindowDoorInWidth));
		model.setUnderWindowDoorLength(getValue(underWindowDoorLength));
		model.setUnderWindowDoorOutLength(getValue(underWindowDoorOutLength));
		model.setUnderWindowDoorOutWidth(getValue(underWindowDoorOutWidth));
		model.setUnderWindowDoorWidth(getValue(underWindowDoorWidth));
		model.setUnderWindowWindowInLength(getValue(underWindowWindowInLength));
		model.setUnderWindowWindowInWidth(getValue(underWindowWindowInWidth));
		model.setUnderWindowWindowOutLength(getValue(underWindowWindowOutLength));
		model.setUnderWindowWindowOutWidth(getValue(underWindowWindowOutWidth));
		model.setWindowEbbDownLength(getValue(windowEbbDownLength));
		model.setWindowEbbDownWidth(getValue(windowEbbDownWidth));
		model.setWindowHeight(getValue(windowHeight));
		model.setWindowWidth(getValue(windowWidth));
		model.setWorkPrice(getValue(workPrice));
		model.setUnderwindowGroupType(getValue(underWindowGroupType));
		model.setDoorType(getValue(doorType));
		model.setAditionalTypeWindow(getValue(additionalWindowType));
		model.setUnderWindowWidth(getValue(underWindowWidth));
		model.setUnderWindowLength(getValue(underWindowLength));

		return model;
	}

	@Override
	protected void init(CommonModel commonModel) {
		BalconyBlockModel model = (BalconyBlockModel) commonModel;
		setValue(countDeafParts, model.getCountDeafParts());
		setValue(countSimpleOpen, model.getCountSimpleOpen());
		setValue(countWithWind, model.getCountWithWind());
		setValue(doorHeight, model.getDoorHeight());
		setValue(doorWidth, model.getDoorWidth());
		setValue(typeDoorGroup, model.getTypeDoorGroup());
		setValue(typeOpenWindowGroup, model.getTypeOpenWindowGroup());
		setValue(typeWindowGroup, model.getTypeWindowGroup());
		setValue(underWindowDoorInLength, model.getUnderWindowDoorInLength());
		setValue(underWindowDoorInWidth, model.getUnderWindowDoorInWidth());
		setValue(underWindowDoorLength, model.getUnderWindowDoorLength());
		setValue(underWindowDoorOutLength, model.getUnderWindowDoorOutLength());
		setValue(underWindowDoorOutWidth, model.getUnderWindowDoorOutWidth());
		setValue(underWindowDoorWidth, model.getUnderWindowDoorWidth());
		setValue(underWindowWindowInLength, model.getUnderWindowWindowInLength());
		setValue(underWindowWindowInWidth, model.getUnderWindowWindowInWidth());
		setValue(underWindowWindowOutLength, model.getUnderWindowWindowOutLength());
		setValue(underWindowWindowOutWidth, model.getUnderWindowWindowOutWidth());
		setValue(windowEbbDownLength, model.getWindowEbbDownLength());
		setValue(windowEbbDownWidth, model.getWindowEbbDownWidth());
		setValue(windowWidth, model.getWindowWidth());
		setValue(windowHeight, model.getWindowHeight());
		setValue(workPrice, model.getWorkPrice());
		setValue(underWindowGroupType, model.getUnderwindowGroupType());
		setValue(doorType, model.getDoorType());
		setValue(additionalWindowType, model.getAditionalTypeWindow());
		setValue(underWindowWidth, model.getUnderWindowWidth());
		setValue(underWindowLength, model.getUnderWindowLength());
	}

	@Override
	protected TypeOrder getTypeOrder() {
		return TypeOrder.BALCONY_BLOOCK;
	}

	@Override
	protected void clear() {
		setEmptyValue(countDeafParts);
		setEmptyValue(countSimpleOpen);
		setEmptyValue(countWithWind);
		setEmptyValue(doorHeight);
		setEmptyValue(doorWidth);
		setEmptyValue(typeDoorGroup);
		setEmptyValue(typeOpenWindowGroup);
		setEmptyValue(typeWindowGroup);
		setEmptyValue(underWindowLength);
		setEmptyValue(underWindowWidth);
		setEmptyValue(underWindowDoorInLength);
		setEmptyValue(underWindowDoorInWidth);
		setEmptyValue(underWindowDoorLength);
		setEmptyValue(underWindowDoorOutLength);
		setEmptyValue(underWindowDoorOutWidth);
		setEmptyValue(underWindowDoorWidth);
		setEmptyValue(underWindowWindowInLength);
		setEmptyValue(underWindowWindowInWidth);
		setEmptyValue(underWindowWindowOutLength);
		setEmptyValue(underWindowWindowOutWidth);
		setEmptyValue(windowEbbDownLength);
		setEmptyValue(windowEbbDownWidth);
		setEmptyValue(windowWidth);
		setEmptyValue(windowHeight);
		setEmptyValue(workPrice);
		setEmptyValue(underWindowGroupType);
		setEmptyValue(doorType);
		setEmptyValue(additionalWindowType);
	}

}
